<?php
class Article extends AdminController {
    public function __construct() {
        parent::__construct();
        session_start();
        $_SESSION["KCFINDER"]["disabled"]=false;
        $_SESSION["KCFINDER"]["uploadURL"]=base_url()."uploads/articles/";
    }
    
    public function index() {
        $this->_data["loadPage"]="article/index_view";
        $this->_data["titlePage"]="Quản lý bài viết"; 
        $this->_data["mess_succ"]=$this->session->flashdata("flash_mess");
        
        $this->session->unset_userdata("sess_search_title");
        $this->session->unset_userdata("sess_search_datefrom");
        $this->session->unset_userdata("sess_search_dateto");
        $this->session->unset_userdata("sess_search_author");
        $this->session->unset_userdata("sess_search_username");
        $this->session->unset_userdata("sess_search_cate");
        $this->session->unset_userdata("sess_search_status");
        
        $this->load->model("marticle");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/article/index/",
            "total_rows"=>$this->marticle->countAll(),
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->marticle->listAll($config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->marticle->listAll($config["per_page"], $start);
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function search() {        
        $this->_data["loadPage"]="article/search_view";
        $this->_data["titlePage"]="Quản lý bài viết";
        
        $title=$this->session->userdata("sess_search_title");
        $datefrom=$this->session->userdata("sess_search_datefrom");
        $dateto=$this->session->userdata("sess_search_dateto");
        $author=$this->session->userdata("sess_search_author");
        $username=$this->session->userdata("sess_search_username");
        $cate=$this->session->userdata("sess_search_cate");
        $status=$this->session->userdata("sess_search_status");
        
        $this->load->helper("menu_cate_helper");
        $this->load->model("mcategory");
        if($this->mcategory->listAll()!=false)
            $this->_data["info_cate"]=$this->mcategory->listAll();
        
        $this->load->helper("status_helper");
        $this->load->model("mstatus");
        if($this->mstatus->listAll()!=false)
            $this->_data["info_status"]=$this->mstatus->listAll();
        
        $this->load->model("marticle");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/article/search/",
            "total_rows"=>$this->marticle->countAllResults($title, $datefrom, $dateto, $author, $username, $cate, $status)["count"],
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->marticle->search($title, $datefrom, $dateto, $author, $username, $cate, $status, $config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->marticle->search($title, $datefrom, $dateto, $author, $username, $cate, $status, $config["per_page"], $start);
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function searchdata() {
        if(isset($_POST["btnSearchArticle"])) {
            $sess=array(
                "sess_search_title"=>$this->input->post("txtTitle"),
                "sess_search_datefrom"=>$this->input->post("txtDateFrom"),
                "sess_search_dateto"=>$this->input->post("txtDateTo"),
                "sess_search_author"=>$this->input->post("txtAuthor"),
                "sess_search_username"=>$this->input->post("txtUsername"),
                "sess_search_cate"=>$this->input->post("slCate"),
                "sess_search_status"=>$this->input->post("slStatus")
            );
            $this->session->set_userdata($sess);
            redirect(base_url()."admin_combo/article/search");
        }
    }
    
    public function add() {
        $this->_data["loadPage"]="article/add_view";
        $this->_data["titlePage"]="Thêm bài viết";
        
        $this->load->helper("menu_cate_helper");
        $this->load->model("mcategory");
        if($this->mcategory->listAll()!=false)
            $this->_data["info_cate"]=$this->mcategory->listAll();
        
        //validate
        $this->load->helper("form");
        $this->load->library("form_validation");
        $this->form_validation->CI =& $this;
        
        $config=array(
            array(
                "field"=>"txtTitle",
                "label"=>"Tên",
                "rules"=>"required"
            )
        );
        $this->form_validation->set_rules($config);
        
        //add
        if($this->form_validation->run()) {
            $this->load->model("marticle");
            $this->load->model("mstatus");
            if($this->mstatus->getByName("chưa duyệt")!=false) {
                $s=$this->mstatus->getByName("chưa duyệt");
                $status_id=$s["status_id"];
            }
            $data=array(
                "art_title"=>$this->input->post("txtTitle"),
                "art_info"=>$this->input->post("txtInfo"),
                "art_detail"=>$this->input->post("txtDetail"),
                "art_date"=>date("Y-m-d H:i:s"),
                "art_author"=>$this->input->post("txtAuthor"),
                "status_id"=>$status_id,
                "cate_id"=>$this->input->post("slCate"),
                "u_id"=>$this->session->userdata("username")
            );
            $data["u_id"]=$this->session->userdata("id");
            
            $flag_file=true;
            if($_FILES["fileImage"]["name"]!="") {
                $config_upload=array(
                    "upload_path"=>"./uploads/article_main/",
                    "allowed_types"=>"gif|jpg|png",
                    "max_size"=>"600",
                    "max_width"=>"1024",
                    "max_height"=>"1024"
                );
                $this->load->library("upload");
                $this->upload->initialize($config_upload);
                if($this->upload->do_upload("fileImage")) {
                    $upload_data=$this->upload->data();
                    $data["art_img"]=$upload_data["file_name"];
                }else {
                    $flag_file=false;
                    $this->_data["error"]=$this->upload->display_errors();
                }
            }

            if($flag_file) {
                $this->marticle->insert($data);
                $this->session->set_flashdata("flash_mess", "Thêm bài viết thành công.");
                redirect(base_url()."admin_combo/article/");
            }
        }
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function edit() {
        $this->_data["loadPage"]="article/edit_view";
        $this->_data["titlePage"]="Chỉnh sửa bài viết";
        
        $this->load->helper("menu_cate_helper");
        $this->load->model("mcategory");
        if($this->mcategory->listAll()!=false)
            $this->_data["info_cate"]=$this->mcategory->listAll();
        
        $this->load->helper("status_helper");
        $this->load->model("mstatus");
        if($this->mstatus->listAll()!=false)
            $this->_data["info_status"]=$this->mstatus->listAll();
        
        //load data
        $id=$this->uri->segment(4);
        $this->load->model("marticle");
        if($this->marticle->getById($id)!=false) {
            $this->_data["info_edit"]=$this->marticle->getById($id);
        }
        
        //validate
        $this->load->helper("form");
        $this->load->library("form_validation");
        $this->form_validation->CI =& $this;
        
        $config=array(
            array(
                "field"=>"txtTitle",
                "label"=>"Tên",
                "rules"=>"required"
            )
        );
        $this->form_validation->set_rules($config);
        
        //add
        if($this->form_validation->run()) {
            $this->load->model("mstatus");
            $data=array(
                "art_title"=>$this->input->post("txtTitle"),
                "art_info"=>$this->input->post("txtInfo"),
                "art_detail"=>$this->input->post("txtDetail"),
                //"art_date"=>date("Y-m-d H:i:s"),
                "art_author"=>$this->input->post("txtAuthor"),
                "status_id"=>$this->input->post("slStatus"),
                "cate_id"=>$this->input->post("slCate")
            );
            
            $flag_file=true;
            if($_FILES["fileImage"]["name"]!="") {
                $config_upload=array(
                    "upload_path"=>"./uploads/article_main/",
                    "allowed_types"=>"gif|jpg|png",
                    "max_size"=>"600",
                    "max_width"=>"1024",
                    "max_height"=>"1024"
                );
                $this->load->library("upload");
                $this->upload->initialize($config_upload);
                if($this->upload->do_upload("fileImage")) {
                    $upload_data=$this->upload->data();
                    $data["art_img"]=$upload_data["file_name"];
                    $old_img=$this->input->post("txtOldImg");
                    unlink("./uploads/article_main/".$old_img);
                }else {
                    $flag_file=false;
                    $this->_data["error"]=$this->upload->display_errors();
                }
            }

            if($flag_file) {
                $this->marticle->update($data, $id);
                $this->session->set_flashdata("flash_mess", "Chỉnh sửa bài viết thành công.");
                redirect(base_url()."admin_combo/article/");
            }
        }
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function del() {
        $id=$this->uri->segment(4);
        $this->load->model("marticle");
        $art=$this->marticle->getById($id);
        $old_img=$art["art_img"];
        unlink("./uploads/article_main/".$old_img);
        $this->marticle->del($id);
        $this->session->set_flashdata("flash_mess", "Xóa bài viết thành công.");
        redirect(base_url()."admin_combo/article/");
    }
}
?>
